package com.atguigu.spring5.dao;

import com.atguigu.spring5.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;


@Repository
public class BookDaoImpl implements BookDao {

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Book book) {
        //1.创建sql语句
        String sql = "insert into t_book values(?,?,?)";

        //2.调用方法实现
        Object[] args = {book.getUserId(), book.getUserName(), book.getUstatus()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);

    }

    //修改
    @Override
    public void updateBook(Book book) {

        String sql = "update t_book set user_name=?, ustatus=? where user_id=?";

        Object[] args = {book.getUserName(), book.getUstatus(), book.getUserId()};

        int update = jdbcTemplate.update(sql, args);

        System.out.println(update);
    }

    //删除
    @Override
    public void delete(Long id) {

        String sql = "delete from t_book where user_id=?";

        int update = jdbcTemplate.update(sql, id);

        System.out.println(update);


    }

    //查询表记录数
    @Override
    public int selectCount() {

        String sql = "select count(1) from t_book";

        //第一个参数:sql语句
        //第二个参数:返回类型
        Integer queryForObject = jdbcTemplate.queryForObject(sql, Integer.class);

        return queryForObject;
    }

    @Override
    public Book findBookInfo(Long id) {

        String sql = "select * from t_book where user_id = ?";

        //调用方法
        //有三个参数:
        //第一个参数:sql语句
        //第二个参数:RowMapper 是接口,返回不同类型数据，使用这个接口里面实现类完成数据的封住
        //第三个参数:sql语句值
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);

        return book;
    }

    @Override
    public List<Book> findAll() {

        String sql = "select * from t_book";

        //调用方法
        List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));

        return bookList;
    }

    //批量添加
    @Override
    public void batchInsert(List<Object[] > list) {
        String sql = "insert into t_book values(?,?,?)";

        int[] ints = jdbcTemplate.batchUpdate(sql, list);

        System.out.println(Arrays.toString(ints));

    }

    //批量修改
    @Override
    public void batchUpdate(List<Object[]> list) {
        String sql = "update t_book set user_name=?, ustatus=? where user_id=?";
        int[] batchUpdate = jdbcTemplate.batchUpdate(sql, list);
        System.out.println(Arrays.toString(batchUpdate));
    }


    //批量删除
    @Override
    public void batchDelete(List<Object[]> batchArgs) {
        String sql = "delete from t_book where user_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println(Arrays.toString(ints));
    }

}
